package com.your.netty.sample.biz.services;

import org.apache.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.your.netty.sample.biz.BizService;
import com.your.netty.sample.core.Application;
import com.your.netty.sample.core.RequestData;
import com.your.netty.sample.protobuf.Sample4Java;
import com.your.netty.sample.server.ResponseMessage;

@Service
public class NettyJavaSampleBizService implements BizService {
	private static final Logger logger = Logger.getLogger(NettyJavaSampleBizService.class);

	public ResponseMessage execute(RequestData requestData) {
		logger.debug("execute start");
		RabbitTemplate rabbitTemplate = (RabbitTemplate) Application.ctx.getBean("rabbitTemplate");
		// protobuff编码
		Sample4Java.UserInfo.Builder builder = Sample4Java.UserInfo.newBuilder();
		JSONObject http_body = JSONObject.parseObject(requestData.getHttp_body());
		builder.setUserName(http_body.getString("userName"));
		// rabbitMQ
		rabbitTemplate.send("your.exchange.name", "your.queue.name", new Message(
				builder.build().toByteArray(),
				new MessageProperties()));
		logger.debug("rabbitMQ is ok");
		// 响应
		JSONObject json = new JSONObject();
		json.put("biz_deal_result", "true");
		return new ResponseMessage(true, json);
	}
}
